package com.webwork.lims.stock.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.webwork.lims.stock.dao.ProductDao;
import com.webwork.lims.stock.dataobject.Product;
import com.webwork.lims.stock.service.ProductService;
import com.webwork.lims.util.Paging;

@Service
public class ProductServiceImpl implements ProductService {

	@Autowired
	private ProductDao productDao;

	@Override
	public List<Product> query(Product queryObj) {
		return this.query(queryObj, new Paging(0, 0));
	}

	@Override
	public int count(Product queryObj) {
		return productDao.count(queryObj);
	}

	@Override
	public List<Product> query(Product queryObj, Paging paging) {
		return productDao.query(queryObj, paging.getCurrPage(), paging.getPageSize());
	}

	@Override
	public Product get(String sku) {
		Product queryObj = new Product();
		queryObj.setSku(sku);
		List<Product> products = this.query(queryObj);
		if (products != null && products.size() > 0) {
			if (products.size() > 1) {
				System.out.println("存在不止一条记录!");
			} else {
				return products.get(0);
			}
		} else {
			System.out.println("找不到该运单!");
		}
		return null;
	}

	@Override
	public void insert(Product product) {
		productDao.insert(product);
	}

	@Override
	public void update(Product product) {
		productDao.update(product);
	}

	@Override
	public void save(Product product) {
		if (this.get(product.getSku()) != null) {
			this.update(product);
		} else {
			this.insert(product);
		}
	}

	@Override
	public void delete(String sku) {
		productDao.delete(sku);
	}
	
}
